/**
 *    @file       endianness_trans.h
 *
 *    @brief      
 *
 *    @details
 *
 *    @version    1.0
 *    @date       12/10/2012 03:16:52 PM
 *
 *    @author     wangtiefeng (), wangtf418@gmail.com
 */
#ifndef _BF_ENDIANNESS_TRANS_H
#define _BF_ENDIANNESS_TRANS_H
#include <netinet/in.h>

#define bf_ntohs(x) ntohs(x)
#define bf_htons(x) htons(x)

#define bf_ntohl(x) ntohl(x)
#define bf_htonl(x) htonl(x)

#if __BYTE_ORDER == __BIG_ENDIAN
	#define bf_ntohll(x) (x)
	#define bf_htonll(x) (x)
#else
	#if __BYTE_ORDER == __LITTLE_ENDIAN
		#define bf_ntohll(x) __bswap_64(x)
		#define bf_htonll(x) __bswap_64(x)
	#endif
#endif

class endianness_trans
{
public:
	endianness_trans()
		: m_trans_flag(1)
	{

	}

	endianness_trans(bf_int_t trans_flag)
		: m_trans_flag(trans_flag)
	{

	}

	virtual ~endianness_trans()
	{

	}
public:
	inline void set_trans_flag(bf_int_t trans_flag) {m_trans_flag = trans_flag;}
	inline bf_int_t get_trans_flag() {return m_trans_flag;}

public:
	inline bf_int8_t trans_int8(bf_int8_t num) {return num;}
	inline bf_int16_t trans_int16(bf_int16_t num) {return (m_trans_flag)? bf_htons(num):num;}
	inline bf_int32_t trans_int32(bf_int32_t num) {return (m_trans_flag)? bf_htonl(num):num;}
	inline bf_int64_t trans_int64(bf_int64_t num) {return (m_trans_flag)? bf_htonll(num):num;}

	inline bf_int8_t trans_int(bf_int8_t num) {return num;}
	inline bf_int16_t trans_int(bf_int16_t num) {return (m_trans_flag)? bf_htons(num):num;}
	inline bf_int32_t trans_int(bf_int32_t num) {return (m_trans_flag)? bf_htonl(num):num;}
	inline bf_int64_t trans_int(bf_int64_t num) {return (m_trans_flag)? bf_htonll(num):num;}

protected:
	bf_int_t m_trans_flag;
};

#endif

